﻿using System;
using System.Collections.Generic;
using UnityEngine;
using XLua;



[LuaCallCSharp]
public class LoadMrg : Singleton<LoadMrg>
{
    public string suffix = ".assetBundle";//脚本后缀

    public string scriptRoot = "luascripts";//脚本跟目录

    private Dictionary<string,AssetBundle> scriptsBundles = new Dictionary<string, AssetBundle>();
    private Dictionary<string, AssetBundle> prefabBundles = new Dictionary<string, AssetBundle>();

    public T Load<T>(string path)where T : UnityEngine.Object
    {
        return Resources.Load<T>(path);
    }
    public T[] LoadAll<T>(string path) where T : UnityEngine.Object
    {
        return Resources.LoadAll<T>(path);
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="packageName"></param>
    /// <param name="assetName"></param>
    /// <param name="onProgress"></param>
    /// <param name="complete"></param>
    public AssetBundle LoadAssetBundleScripts(string packageName)
    {
        string path = PathUnit.GetWWWPath() + "/" + PathUnit.GetScriptAssetBundlePath(packageName + suffix);
        path = path.ToLower();
        if (scriptsBundles.ContainsKey(path)) return scriptsBundles[path];
        AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
        scriptsBundles.Add(path, assetBundle);
        return assetBundle;
    }
    public AssetBundle LoadAssetPkg(string packageName)
    {
        string path = PathUnit.GetWWWPath() + "/" + packageName + suffix;
        path = path.ToLower();
        if(prefabBundles.ContainsKey(path)) return prefabBundles[path];
        else
        {
            AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
            prefabBundles.Add(path, assetBundle);
            return assetBundle;
        }
    } 
    public bool UnLoadAssetPkg(string packageName,bool unloadAllLoadedObjects=false)
    {
        string path = PathUnit.GetWWWPath() + "/" + packageName + suffix;
        path = path.ToLower();
        if (prefabBundles.ContainsKey(path))
        {
            prefabBundles[path].Unload(unloadAllLoadedObjects);
            prefabBundles.Remove(path);
            return true;
        }
        return false;
    }

   
    public UnityEngine.Object LoadAsset(AssetBundle assetBundle,string assetName,Type type)
    {
        if(assetBundle == null)return null;
        assetName= assetName.ToLower();
        if (type == null) return assetBundle.LoadAsset(assetName);
        return assetBundle.LoadAsset(assetName, type);
    }
    public UnityEngine.Object[] LoadAllAssets(AssetBundle assetBundle, Type type)
    {
        if (assetBundle == null) return null;
        if (type == null)return assetBundle.LoadAllAssets();
        return assetBundle.LoadAllAssets(type);
    }
    public UnityEngine.Object[] LoadAssetWithSubAssets(AssetBundle assetBundle, string assetName, Type type)
    {
        if (assetBundle == null) return null;
        assetName = assetName.ToLower();
        if (type == null) return assetBundle.LoadAssetWithSubAssets(assetName);
        return assetBundle.LoadAssetWithSubAssets(assetName,type);
    }
}